package org.user.dynamicFeignClient;

import com.alibaba.cloud.sentinel.feign.SentinelFeign;
import feign.Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignCircuitBreaker;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.cloud.openfeign.FeignClientFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;


@Component
public class DynamicFeignClient<T> {
    private FeignClientBuilder feignClientBuilder;

    @Autowired
    ApplicationContext appContext;

    public DynamicFeignClient( @Autowired ApplicationContext appContext) {
        this.feignClientBuilder = new FeignClientBuilder(appContext);
    }


        /**
         * 动态调用地址
         *
         * @param type      接口
         * @param serviceId 服务名称
         * 被调用的服务如果设置了server.servlet.context-path的属性,默认与spring.application.name的值相同,赋值给path
         * 如果server.servlet.context-path与spring.application.name不同,则调用另外一个方法
         * @return
         */
    public T GetFeignClient(final Class<T> type, String serviceId) {
       T df= this.feignClientBuilder.forType(type,serviceId)
                .path(serviceId).build();
        return df;
    }


    /**
     * 动态调用地址
     *
     * @param type      接口
     * @param serviceId 服务名称
     * @param serverPath server.servlet.context-path的值
     * @return
     */
    public T GetFeignClient(final Class<T> type, String serviceId,String serverPath) {
        T df= this.feignClientBuilder.forType(type,serviceId)
                .path(serverPath).build();
        return df;
    }
}